home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / AVIOutput.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  6.2 KB  |  218 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #ifndef f_AVIOUTPUT_H
  19. #define f_AVIOUTPUT_H
  20.  
  21. #include <windows.h>
  22. #include <vfw.h>
  23.  
  24. #include "Fixes.h"
  25.  
  26. class AVIIndex;
  27. class AudioSource;
  28. class VideoSource;
  29. class FastWriteStream;
  30. class AVIIndexEntry2;
  31. typedef struct _avisuperindex_chunk AVISUPERINDEX;
  32. struct _avisuperindex_entry;
  33.  
  34. //////////////////////////
  35.  
  36. class AVIOutputStream {
  37. private:
  38.     LONG                formatLen;
  39.     void                *format;
  40.  
  41. protected:
  42.     class AVIOutput        *output;
  43.     BOOL _write(FOURCC ckid, LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer);
  44.  
  45. public:
  46.     AVIStreamHeader_fixed        streamInfo;
  47.  
  48.     AVIOutputStream(class AVIOutput *output);
  49.     virtual ~AVIOutputStream();
  50.  
  51.     void *allocFormat(LONG len) {
  52.         if (format && formatLen == len) return format;
  53.  
  54.         delete format;
  55.         return format = new char[formatLen = len];
  56.     }
  57.     void *getFormat() { return format; }
  58.     LONG getFormatLen() { return formatLen; }
  59.  
  60.     virtual BOOL write(LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer, LONG lSamples) = 0;
  61.     virtual BOOL finalize();
  62.  
  63.     LONG msToSamples(LONG lMs) {
  64.         return (LONG)(((__int64)lMs * streamInfo.dwRate) / ((__int64)1000 * streamInfo.dwScale));
  65.     }
  66.     LONG samplesToMs(LONG lSamples) {
  67.         return (LONG)((((__int64)lSamples * streamInfo.dwScale) * 1000) / streamInfo.dwScale);
  68.     }
  69. };
  70.  
  71. class AVIAudioOutputStream : public AVIOutputStream {
  72. public:
  73.     LONG lTotalSamplesWritten;
  74.  
  75.     AVIAudioOutputStream(class AVIOutput *out);
  76.  
  77.     WAVEFORMATEX *getWaveFormat() { return (WAVEFORMATEX *)getFormat(); }
  78.  
  79.     BOOL write(LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer, LONG lSamples);
  80.     virtual BOOL finalize();
  81.     virtual BOOL flush();
  82. };
  83.  
  84. class AVIVideoOutputStream : public AVIOutputStream {
  85. public:
  86.     LONG lTotalSamplesWritten;
  87.     FOURCC id;
  88.  
  89.     AVIVideoOutputStream(class AVIOutput *out);
  90.  
  91.     BITMAPINFOHEADER *getImageFormat() { return (BITMAPINFOHEADER *)getFormat(); }
  92.  
  93.     void setCompressed(BOOL x) { id = x ? mmioFOURCC('0','0','d','c') : mmioFOURCC('0','0','d','b'); }
  94.     BOOL isCompressed() { return id == mmioFOURCC('0','0','d','c'); }
  95.  
  96.     BOOL write(LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer, LONG lSamples);
  97.     virtual BOOL finalize();
  98. };
  99.  
  100. class AVIOutput {
  101. protected:
  102.     static char szME[];
  103. public:
  104.     AVIAudioOutputStream    *audioOut;
  105.     AVIVideoOutputStream    *videoOut;
  106.  
  107.     AVIOutput();
  108.     virtual ~AVIOutput();
  109.  
  110.     virtual BOOL initOutputStreams()=0;
  111.     virtual BOOL init(const char *szFile, LONG xSize, LONG ySize, BOOL videoIn, BOOL audioIn, LONG bufferSize, BOOL is_interleaved)=0;
  112.     virtual BOOL finalize()=0;
  113.     virtual BOOL isPreview()=0;
  114.  
  115.     virtual void writeIndexedChunk(FOURCC ckid, LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer)=0;
  116. };
  117.  
  118. class AVIOutputFile : public AVIOutput {
  119. private:
  120.     enum { MAX_AVIBLOCKS = 64, MAX_SUPERINDEX_ENTRIES=256, MAX_INDEX_ENTRIES=3072 };
  121.  
  122.     FastWriteStream *fastIO;
  123.     HANDLE        hFile;
  124.     __int64        i64FilePosition;
  125.     __int64        i64XBufferLevel;
  126.  
  127.     __int64        avi_movi_pos[64];
  128.     __int64        avi_movi_len[64];
  129.     __int64        avi_riff_pos[64];
  130.     __int64        avi_riff_len[64];
  131.     int            xblock;
  132.  
  133.     long        strl_pos;
  134.     long        misc_pos;
  135.     long        main_hdr_pos;
  136.     long        audio_hdr_pos;
  137.     long        audio_format_pos;
  138.     long        video_hdr_pos;
  139.     long        audio_indx_pos;
  140.     long        video_indx_pos;
  141.     long        dmlh_pos;
  142.     long        seghint_pos;
  143.  
  144.     int            chunkFlags;
  145.  
  146.     AVIIndex    *index, *index_audio, *index_video;
  147.     char *        pHeaderBlock;
  148.     long        nHeaderLen;
  149.  
  150.     MainAVIHeader        avihdr;
  151.  
  152.     long        lChunkSize;
  153.     long        lAVILimit;
  154.     int            iPadOffset;
  155.  
  156.     bool        fCaching;
  157.     bool        fExtendedAVI;
  158.     bool        fCaptureMode;
  159.     bool        fInitComplete;
  160.  
  161.     char *        pSegmentHint;
  162.     int            cbSegmentHint;
  163.  
  164.     __int64        i64EndOfFile;
  165.     __int64        i64FarthestWritePoint;
  166.     long        lLargestIndexDelta[2];
  167.     __int64        i64FirstIndexedChunk[2];
  168.     __int64        i64LastIndexedChunk[2];
  169.     bool        fLimitTo4Gb;
  170.     long        lIndexedChunkCount[2];
  171.     long        lIndexSize;
  172.     bool        fPreemptiveExtendFailed;
  173.  
  174.     BOOL        _init(const char *szFile, LONG xSize, LONG ySize, BOOL videoIn, BOOL audioIn, LONG bufferSize, BOOL is_interleaved, bool fThreaded);
  175.  
  176.     __int64        _writeHdr(void *data, long len);
  177.     __int64        _beginList(FOURCC ckid);
  178.     __int64        _writeHdrChunk(FOURCC ckid, void *data, long len);
  179.     void        _closeList(__int64 pos);
  180.     void        _flushHdr();
  181.     __int64        _getPosition();
  182.     void        _seekHdr(__int64 i64NewPos);
  183.     bool        _extendFile(__int64 i64NewPoint);
  184.     void        _seekDirect(__int64 i64NewPos);
  185.     __int64        _writeDirect(void *data, long len);
  186.  
  187.     void        _write(void *data, int len);
  188.     void        _closeXblock();
  189.     void        _openXblock();
  190.     void        _writeLegacyIndex(bool use_fastIO);
  191.  
  192.     void        _createNewIndices(AVIIndex *index, AVISUPERINDEX *asi, _avisuperindex_entry *asie, bool is_audio);
  193.     int            _writeNewIndex(struct _avisuperindex_entry *asie, AVIIndexEntry2 *avie2, int size, FOURCC fcc, DWORD dwChunkId, DWORD dwSampleSize);
  194. public:
  195.     AVIOutputFile();
  196.     virtual ~AVIOutputFile();
  197.  
  198.     void disable_os_caching();
  199.     void disable_extended_avi();
  200.     void set_1Gb_limit();
  201.     void set_chunk_size(long cs);
  202.     void set_capture_mode(bool b);
  203.     void setSegmentHintBlock(bool fIsFinal, const char *pszNextPath, int cbBlock);
  204.  
  205.     BOOL initOutputStreams();
  206.     BOOL init(const char *szFile, LONG xSize, LONG ySize, BOOL videoIn, BOOL audioIn, LONG bufferSize, BOOL is_interleaved);
  207.     FastWriteStream *initCapture(const char *szFile, LONG xSize, LONG ySize, BOOL videoIn, BOOL audioIn, LONG bufferSize, BOOL is_interleaved);
  208.  
  209.     BOOL finalize();
  210.     BOOL isPreview();
  211.  
  212.     void writeIndexedChunk(FOURCC ckid, LONG dwIndexFlags, LPVOID lpBuffer, LONG cbBuffer);
  213.  
  214.     LONG bufferStatus(LONG *lplBufferSize);
  215. };
  216.  
  217. #endif
  218.